<?php
namespace app\gym\controller;
use app\common\controller\AdminBase;
/*工作人员*/
class Employee extends AdminBase{
	private $shop_id;
	protected $beforeActionList = [
        'before_index'=>['only'=>'index'],
    ];
	function _initialize() {
		parent::_initialize();
		$this->shop_id=sp_get_current_shop_id();
    }
	/*列表*/
	function index(){
	  	$this->assign("type",input("type"));
		if(input("type")==2){//会籍mc
			echo $this->fetch("index_2");
		}elseif(input("type")==3){
			echo $this->fetch("index_3");
		}
	}	
	//删除
	function delete(){
		$id=input("id");
		db("GymEmployee")->where("id =$id and shop_id=".$this->shop_id)->delete();
		$this->ajax_success("成功");
	}
	//编辑
	function edit(){
		$model=db("GymEmployee")->where("shop_id=".$this->shop_id." and id=".input("id"))->find();
		$this->assign("model",$model);
		if(input("type")==2){
			echo $this->fetch("edit_2");
		}else if(input("type")==3){
			echo $this->fetch("edit_3");			
		}
	}
	function edit_post(){
		if(request()->isPost()){//提交
		    $data=array(
							   "name"=>input("name"),
							   "sex"=>input("sex"),
							   "descs"=>input("descs"),
							   "position"=>input("position"),
							   "web_open_up"=>input("web_open_up",0),
						);
			if(input("old_mobile")!=input("mobile")){
				$shop_id=sp_get_current_shop_id();
				$count=db("GymEmployee")->where("mobile='".input("mobile")."' and shop_id=$shop_id")->count(1);
				if($count>0){
					$this->ajax_error("手机号码存在!");
				}else{
					$data['mobile']=input("mobile");
				}
			}
						
			if(input("type")==3){
				$data['coash_type']=input("coash_type");
			}			
			db("GymEmployee")->where("shop_id=".$this->shop_id." and id=".input("id"))->update($data);
			$this->ajax_success("成功");
		}
	}
	//新增
	function add(){
		if(request()->isPost()){
			$mobile=input("mobile");
			$shop_id=sp_get_current_shop_id();
			$count=db("GymEmployee")->where("mobile='$mobile' and shop_id=$shop_id")->count(1);
			if($count>0){
				$this->ajax_error("手机号码存在!");
			}
			db("GymEmployee")->insert(
						array(
							"name"=>input("name"),
							"shop_id"=>$this->shop_id,
							"mobile"=>$mobile,
							"sex"=>input("sex"),
							"type"=>input("type"),
							"coash_type"=>input("coash_type",0),
							"descs"=>input("descs"),
							"position"=>input("position"),
							"web_open_up"=>input("web_open_up",0),
							));
		    $this->ajax_success("成功");
		}else{
			if(input("type")==2){
				echo $this->fetch("add_2");
			}else if(input("type")==3){
				echo $this->fetch("add_3");			
			}
			die;
		}
	}
	//tab
	function step(){
		echo $this->fetch();
	}
	
	/*卡列表数据*/
	function ajax_index(){
		$this->ajax($this->_ajax_index());
	}
	
	///////////////////////////////////////////////////////////////////////////////////
	/**
	 * 权限菜单
	 **/
	function before_index(){
		if(input("type")==2){//会籍mc
			$auth_group=sp_get_auth_menu_3("10061001");
		}else if(input("type")==3){//教练pt
			$auth_group=sp_get_auth_menu_3("10061002");
		}
		$this->assign("menus",$auth_group);
	}
	/*获取卡数据*/
	private function _ajax_index(){
		$query=array(
		 'name'=>array("field" =>"name","operator" =>"="),
		 'mobile'=>array("field" =>"mobile","operator" =>"="),
		 'type'=>array("field" =>"type","operator" =>"="),
		 );
		$shop_id=$this->shop_id;;
		$draw=input("draw",0)+1;//请求时间		 
		$where_and=join(" and ",sp_get_param_sql(request()->isPost(),$query));
		$where_and.=" and shop_id=$shop_id and status=1";
		$total=db("GymEmployee")->where($where_and)->fetchsql(FALSE)->count(1);
		$list=db("GymEmployee")->where($where_and)->order($this->get_order())->limit($this->get_limit())->fetchsql(FALSE)->select();
		return array('pageData'=>$list,'total'=>$total,"draw"=>$draw);
	}
}
